package com.qgs.rabbitmqapi.ack;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 蔡翔
 * @Date: 2019/7/19 13:27
 * @Version 1.0
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.0.229");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPort(5672);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        String exchange = "test_ack_exchange";
        String routingKey = "ack.save";


        for(int i=0;i<5;i++){
            String msg = "hello rabbitmq ACK message";
            Map<String ,Object> headers = new HashMap<String,Object>();
            headers.put("num",i);
            AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
                    //投递模式为2 说明我们的消息是要持久化的。
                    .deliveryMode(2)
                    .contentEncoding("UTF-8")
                    .headers(headers)
                    .build();
            msg = msg + i;
            channel.basicPublish(exchange,routingKey,true,properties,msg.getBytes());
        }
    }
}